package com.abel.demo.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * Author: Abel.lin
 * Date: Created in 2018/7/13 18:30
 * Company: Abel.Studio
 * Copyright: Copyright (c) 2017
 * Description:
 */
public class SignUtil {
    private static final String CONTENT_CHARSET = "UTF-8";

    private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static MessageDigest messagedigest = null;

    public static String MD5(String s) throws NoSuchAlgorithmException {
        return MD5(s.getBytes());
    }

    public static String MD5(byte[] bytes) throws NoSuchAlgorithmException {
        messagedigest = MessageDigest.getInstance("MD5");
        return bufferToHex(messagedigest.digest(bytes));
    }

    public static String SHA1(String s) throws NoSuchAlgorithmException {
        messagedigest = MessageDigest.getInstance("SHA-1");
        return bufferToHex(messagedigest.digest(s.getBytes()));
    }

    public static String SHA2(String s) throws NoSuchAlgorithmException {
        messagedigest = MessageDigest.getInstance("SHA-256");
        return bufferToHex(messagedigest.digest(s.getBytes()));
    }

    /**
     * @author: Abel.lin
     * @date: 2018/7/16 11:18
     * @description: 计算二进制数据
     * @return:
     */
    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }


    /**
     * author: Abel.lin
     * date: Created in 2018/4/25 14:24
     * description: 参数字典序化
     * return:
     */
    public static String sortDict(Map<String,String> map){
        StringBuffer sb = new StringBuffer();
        Set<String> keySet = map.keySet();
        List<String> keyList = new ArrayList<String>(keySet);
        Collections.sort(keyList);
        for(String key : keyList){
            sb.append("&").append(key).append("=").append(map.get(key));
        }
        if(sb.length() > 1){
            return sb.substring(1);
        }
        return null;
    }

    public static String getRandom(int size){
        Random random = new Random();
        String randStr = "";
        for (int i = 0; i < size; i++) {
            String randItem = String.valueOf(random.nextInt(10));
            randStr += randItem;
        }
        return randStr;
    }

    /**
     * @author: Abel.lin
     * @date: Created in 2018/4/25 18:15
     * @description:
     * @param signStr 被加密串
     * @param secret 加密密钥
     * @return:
     */
    public static String hmacSHA1(String signStr, String secret)throws Exception {
        String sig = null;
        Mac mac = Mac.getInstance("HmacSHA1");
        byte[] hash;

        SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
        mac.init(secretKey);
        hash = mac.doFinal(signStr.getBytes(CONTENT_CHARSET));

        sig = Base64.getEncoder().encodeToString(hash);
        return sig;
    }
}
